from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
from .models import *
from django.core.exceptions import ValidationError,ObjectDoesNotExist
from django.db.utils import IntegrityError
import time

# Create your views here.
def add_event(request):
    eid=request.POST.get('eid')
    name=request.POST.get('name')
    limit=request.POST.get('limit')
    status=request.POST.get('status')
    address=request.POST.get('address')
    start_time=request.POST.get('start_time')
    if eid=='' or name=='' or limit=='' or address=='' or start_time=='':
        return JsonResponse({'status':10021,'message':'parameter error'})
    result=Event.objects.filter(id=eid)
    if result:
        return JsonResponse({'status':10022,'message':'event id already exists'})
    result = Event.objects.filter(name=name)
    if result:
        return JsonResponse({'status': 10023, 'message': 'event name already exists'})
    if status=='':
        status=1
    try:
        Event.objects.create(id=eid,name=name,limit=limit,address=address,status=int(status),start_time=start_time)
    except ValidationError:
        error='start_time format error.lt must be in YYYY-MM-DD HH:MM:SS format'
        return JsonResponse({'status':10024,'message':error})
    return JsonResponse({'status':200,'message':'add event success'})

def get_event_list(request):
    eid=request.GET.get('eid','')
    name=request.GET.get('name','')
    if eid=='' and name=='':
        return JsonResponse({'status':10021,'message':'parameter error'})
    if eid!='':
        event={}
        try:
            result=Event.objects.get(id=eid)
        except ObjectDoesNotExist:
            return JsonResponse({'status':10022,'message':'query result is empty'})
        else:
            event['eid']=result.id
            event['name']=result.name
            event['limit']=result.limit
            event['status']=result.status
            event['address']=result.address
            event['start_time']=result.start_time
            return JsonResponse({'status':200,'message':'success','data':event})
    if name!='':
        datase=[]
        results=Event.objects.filter(name_contains=name)
        if results:
            for r in results:
                event={}
                event['eid']=r.id
                event['name']=r.name
                event['limit']=r.limit
                event['status']=r.status
                event['address']=r.address
                event['start_time']=r.start_time
                datase.append(event)
            return JsonResponse({'status':200,'message':'success','data':datase})
        else:
            return JsonResponse({'status':10022,'message':'query result is empty'})

def add_guest(request):
    eid=request.POST.get('eid')
    realname=request.POST.get('realname')
    phone=request.POST.get('phone')
    email=request.POST.get('email')
    if eid=='' or realname=='' or phone=='' or email=='' :
        return JsonResponse({'status':10021,'message':'parameter error'})
    result = Event.objects.filter(id=eid)
    if not result:
        return JsonResponse({'status': 10022, 'message': 'event id null'})
    result = Event.objects.get(id=eid).status
    if not result:
        return JsonResponse({'status': 10023, 'message': 'event status is not available'})
    event_limit=Event.objects.get(id=eid).limit
    guest_limit=Guest.objects.filter(event_id=eid)
    if len(guest_limit)>=event_limit:
        return JsonResponse({'status':10024,'message':'event number is full'})
    try:
        Guest.objects.create(realname=realname,phone=phone,email=email)
    except BaseException:
        return JsonResponse({'status':10025,'mes':'phone is doble'})
def get_guest_list(request):
    eid=request.GET.get('eid','')
    phone=request.GET.get('phone','')
    if eid=='':
        return JsonResponse({'status': 10021, 'message': 'eid cannot be empty'})
    if eid=='' and phone=='':
        datas=[]
        results=Guest.objects.filter(event_id=eid)
        if results:
            for i in results:
                guest={}
                guest['realname']=i.realname
                guest['phone']=i.phone
                guest['email']=i.email
                guest['sign']=i.sign
                datas.append(guest)
            return JsonResponse({'status': 200, 'message': 'success', 'data': datas})
        else:
            return JsonResponse({'status': 10022, 'message': 'query result is empty'})
    if eid!='' and phone!='':
        guest={}
        try:
            results=Guest.objects.get(phone=phone,event_id=eid)
        except ObjectDoesNotExist:
            return JsonResponse({'status':10022,'mes':'query results is empty'})
        else:
            guest['phone'] = results.phone
            guest['email'] = results.email
            guest['sign'] = results.sign
            return JsonResponse({'status':200,'mes':'success','data':guest})
def signs(request):
    eid=request.GET.get('eid')
    phone=request.GET.get('phone')
    if id=='' and phone=='':
        return JsonResponse({'status':10021,'message':'number is not null'})
    result=Event.objects.filter(id=eid)
    if not result:
        return JsonResponse({'status':10022,'message':'people is donnt have'})
    result=Event.objects.get(id=eid).status
    if not result:
        return JsonResponse({'status':10022,'mes':'已签到'})
    return JsonResponse({'status':200,'mes':'签到成功'})






